/*LICENSE_BEGIN
 * ******************************************************************
 * All original parts of SoccerScope2003 have copyright (2003) by
 * Team YowAI, represented by Mr. Shuhei Shiota
 * The Department of Creative Informatics
 * The Graduate School of Information Science and Technology
 * The University of Tokyo
 * 
 * Additional classes (except libraries in /lib) were developed by 
 * the Dainamite Robocup-Team of the DAI-Labor, TU-Berlin 
 * (www.dai-labor.de)
 * 
 * Extended/new classes/packages are:
 *   soccerscope.analyzer.LogfileAnalyzer
 *   soccerscope.daistatistic.*
 *   soccerscope.daistatistic.charts.*
 *   soccerscope.daistatistic.tools.*
 *   soccerscope.db.*
 *   soccerscope.dialog.*
 *   soccerscope.model.PlayerWorldModel
 *   soccerscope.model.SimplePlayer
 *   soccerscope.net.AgentConnector
 *   soccerscope.training.*
 *   soccerscope.training.conditions.*
 * ******************************************************************
LICENSE_END*/
package soccerscope.file;

import java.io.File;

import javax.swing.filechooser.FileFilter;

public class ServerLogFileFilter extends FileFilter {

	public final static String rcl = "rcl";

	public final static String gz = "rcl.gz";

	public final static String zip = "rcl.zip";

	private String file;

	public ServerLogFileFilter(String file) {
		this.file = file;
	}

	@Override
	public boolean accept(File f) {
		if (f.isDirectory()) {
			return true;
		}

		String s = f.getName();
		int len = s.indexOf(".");
		if (len <= 0)
			return false;
		s = s.substring(0, len);
		if (this.file == null || s.matches(this.file)) {
			String extension = getExtension(f);
			if (extension != null) {
				if (extension.equals(rcl) || extension.equals(gz)
						|| extension.equals(zip)) {
					return true;
				} else {
					return false;
				}
			}
		}

		return false;
	}

	@Override
	public String getDescription() {
		return "RoboCup Server Log";
	}

	/*
	 * Get the extension of a file.
	 */
	public static String getExtension(File f) {
		String ext = null;
		String s = f.getName();
		int i = s.indexOf('.');

		if (i > 0 && i < s.length() - 1) {
			ext = s.substring(i + 1).toLowerCase();
		}
		return ext;
	}
}
